package com.aaa.power.controller;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.power.entity.DictData;
import com.aaa.power.entity.DictType;
import com.aaa.power.service.DictDataService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 字典数据表(DictData)表控制层
 *
 * @author makejava
 * @since 2020-11-16 18:56:19
 */
@RestController
@RequestMapping("dictData")
public class DictDataController {
    /**
     * 服务对象
     */
    @Resource
    private DictDataService dictDataService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public DictData selectOne(Integer id) {
        return this.dictDataService.queryById(id);
    }


    /**
     * 新增
     */
    @PostMapping("insertDictData")
    public Result insertDictData(@RequestBody DictData dictData){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),dictDataService.insertByParam(dictData));
    }

    /**
     * 修改
     */
    @PutMapping("updateDictData")
    public Result updateDictData(@RequestBody DictData dictCode){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),dictDataService.update(dictCode));
    }

    /**
     * 删除
     */
    @DeleteMapping("deleteDictData")
    public Result deleteDictData(Integer dictCode) {
        //当ID为空时抛出自定义异常
        if (null == dictCode) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),dictDataService.deleteById(dictCode));
    }
    /**
     * 分页加模糊检索
     * @param pageNo
     * @param pageSize
     * @param dictData
     * @return
     */
    @GetMapping("queryAll")
    public Result queryAll(Integer pageNo, Integer pageSize, DictData dictData) {
        return dictDataService.queryAll(pageNo,pageSize,dictData);
    }
}